import os
import subprocess
from datetime import datetime
from live.livesystem import LiveSystem
from utils.logger import sys_logger,close_logger

logger = sys_logger.getChild('FuncTest')

def main():

    # 初始化交易系统
    with LiveSystem() as system:
        # 设置终端标题
        start_time = datetime.now()

        # 系统预热（连接交易所、加载策略等）
        system.start()

        system._reset_daily_status()

        #now = datetime(2025, 3, 14, 0, 0, 1, 0)
        now = datetime.now()

        system.engine.on_date_change(now.date())

        system._pre_market()

        system._on_open()

        system._on_trade()

        system._on_close()

        system._post_market()

        logger.info("运行时长: %.2f秒", (datetime.now() - start_time).total_seconds())

def set_terminal_title(self, title: str = ""):
    """设置终端窗口标题（仅限Windows）"""
    try:
        full_title = f"{title} mode: testlive {os.getcwd()}"
        command = f'$Host.UI.RawUI.WindowTitle = "{full_title}"'

        subprocess.run(["powershell", "-Command", command], check=True)
        logger.debug("终端标题已更新: %s", full_title)
    except subprocess.CalledProcessError as e:
        logger.warning("终端标题更新失败: %s", str(e))

if __name__ == "__main__":
    set_terminal_title("QMT FuncTestLiveSystem")
    main()
    close_logger()